package com.hxy.pet.service.impl;

import com.hxy.basic.exception.BusinessExcpetion;
import com.hxy.basic.query.util.LoginContextUtil;
import com.hxy.basic.service.impl.BaseServiceImpl;
import com.hxy.org.domain.Employee;
import com.hxy.org.mapper.EmployeeMapper;
import com.hxy.pet.domain.Pet;
import com.hxy.pet.domain.PetDetail;
import com.hxy.pet.mapper.PetDetailMapper;
import com.hxy.pet.mapper.PetMapper;
import com.hxy.pet.service.IPetService;
import com.hxy.user.domain.LoginInfo;
import com.hxy.user.domain.User;
import com.hxy.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宠物实现类
 */
@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 保存
     */
    @Override
    public void save(Pet pet) {
        //添加宠物信息
        super.save(pet);
        //添加明细
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }

    }


    /**
     * 删除
     */
    @Override
    public void delete(Long id) { //传递的是宠物id
        //删除宠物的信息
        super.delete(id);
        //删除宠物详情
        petDetailMapper.removeByPetId(id);
    }


    /**
     * 批量删除
     */
    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        //删除宠物的信息
        super.batchRemove(ids);
        //删除宠物详情
        petDetailMapper.batchRemoveByPetIds(ids);
    }


    /**
     * 修改
     */
    @Override
    @Transactional
    public void update(Pet pet) {
        //修改宠物的信息
        super.update(pet);
        //修改宠物详情
        PetDetail detail = pet.getDetail();
        if(detail!= null){
            detail.setPet_id(pet.getId());
            petDetailMapper.updateByPetId(detail);
        }
    }
    /**
     * 上架
     */
    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onsaletime",new Date());
        //1.修改state状态 = 1
        //2.设置上架时间onsaletime = new Date();
        petMapper.onSale(map);

    }

    /**
     * 下架
     */
    @Override
    public void offSale(List<Long> ids) {
        //1.修改state状态 = 0
        //2.设置下架时间offsaletime= new Date();
        Map<String,Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offsaletime",new Date());
        petMapper.offSale(map);
    }
    /**
     * 领养宠物
     */
    @Override
    public void adopt(Long id, HttpServletRequest request) {
        Pet pet = petMapper.loadById(id);
        //1.下架
        pet.setState(0);
        //2.设置下架时间
        pet.setOffsaletime(new Date());
        //3.绑定用户id
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        User user = userMapper.findByLoginInfoId(loginInfo.getId());
        pet.setUser_id(user.getId());
        pet.setUser(user);
        //修改新的状态
        petMapper.update(pet);
        //4.生成订单 @TODO
        //5.支付 @TODO

    }

    @Override
    public void isShopAdmin(HttpServletRequest request) {
        //获取管理员
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo.getId());
        //什么管理员
        if(employee.getShop_id() == null){//平台管理员
            throw new BusinessExcpetion("平台管理员");
        }

    }


    @Transactional
    public void add(Pet pet,HttpServletRequest request) {
        //获取管理员 - 设置shop_id
        LoginInfo loginInfo = LoginContextUtil.getLoginInfo(request);
        Employee employee = employeeMapper.findByLoginInfoId(loginInfo.getId());
        pet.setShop_id(employee.getShop_id());
        //添加宠物信息
        super.save(pet);
        //添加明细
        PetDetail detail = pet.getDetail();
        if(detail != null){
            detail.setPet_id(pet.getId());
            petDetailMapper.save(detail);
        }
    }
}
